<?php /** @noinspection DuplicatedCode */

namespace app\admin\controller;

use app\common\lib\verify\EmailVerify;
use app\common\lib\verify\VerifyFactory;


use app\common\lib\WxTemplateMsg;
use hg\apidoc\annotation as Apidoc;
use hg\apidoc\annotation\Param;
use hg\apidoc\annotation\Query;
use hg\apidoc\annotation\Returned;
use hg\apidoc\annotation\Header;
use app\BaseController;
use think\facade\View;
use think\facade\Db;
use think\response\Json;
/**
 * 订单发票
 *
 * admin-OrderInvoice
 *
 * @Apidoc\Title("订单发票")
 * @Apidoc\Group("api")
 */
class OrderInvoice extends BaseController
{
    /**
     * 新增
     *
     * admin-OrderInvoice-add
     */
    public function add()
    {
        if (request()->isPost()) {
            $data = input();
            $data = group_by_table_name($data);
            \app\common\model\OrderInvoice::create($data['order_invoice']);
            return success();
        } else {
            $options = [];
            $order_invoice__is_special_ticket_option = [['name' => '是', 'id' => '1'], ['name' => '否', 'id' => '0']];
            $options['order_invoice__is_special_ticket_options'] = $order_invoice__is_special_ticket_option;
            $order_invoice__is_invoicing_option = [['name' => '是', 'id' => '1'], ['name' => '否', 'id' => '0']];
            $options['order_invoice__is_invoicing_options'] = $order_invoice__is_invoicing_option;
            View::assign($options);
            return View::fetch();
        }
    }
    /**
     * 修改
     *
     * admin-OrderInvoice-update
     */
    public function update()
    {
        if (request()->isPost()) {
            $data = input();
            $data = group_by_table_name($data);
            \app\common\model\OrderInvoice::update($data['order_invoice']);
            return success();
        } else {
            $data = \app\common\model\OrderInvoice::find(input('order_invoice__id'));
            $r = [];
            foreach ($data->getData() as $key => $val) {
                $r['order_invoice__' . $key] = $val;
            }
            View::assign('r', $r);
            $options = [];
            $order_invoice__is_special_ticket_option = [['name' => '是', 'id' => '1'], ['name' => '否', 'id' => '0']];
            $options['order_invoice__is_special_ticket_options'] = $order_invoice__is_special_ticket_option;
            $order_invoice__is_invoicing_option = [['name' => '是', 'id' => '1'], ['name' => '否', 'id' => '0']];
            $options['order_invoice__is_invoicing_options'] = $order_invoice__is_invoicing_option;
            View::assign($options);
            return View::fetch();
        }
    }


    public function update1()
    {
        if (request()->isPost()) {
            $data = input();
            $data = group_by_table_name($data);
            if(!$data['order_invoice']['invoice_attachment']){
                return error('请上传发票附件');
            }
            \app\common\model\OrderInvoice::update($data['order_invoice']);

            $id = $data['order_invoice']['id'];
            $data = \app\common\model\OrderInvoice::find($id);



            WxTemplateMsg::send($data['member_id'], 6, [
                'amount8' => $data['amount_of_money'].'元',
                'time3' => date('Y-m-d', time()),
                'thing1' => '邮箱：' . $data['email'],
            ]);

            $email = new EmailVerify('invoice');
            $email->sendCode($data['email'], $data['invoice_attachment']);

            return success();
        } else {
            $data = \app\common\model\OrderInvoice::find(input('order_invoice__id'));
            $r = [];
            foreach ($data->getData() as $key => $val) {
                $r['order_invoice__' . $key] = $val;
            }
            View::assign('r', $r);
            $options = [];
            $order_invoice__is_special_ticket_option = [['name' => '是', 'id' => '1'], ['name' => '否', 'id' => '0']];
            $options['order_invoice__is_special_ticket_options'] = $order_invoice__is_special_ticket_option;
            $order_invoice__is_invoicing_option = [['name' => '是', 'id' => '1'], ['name' => '否', 'id' => '0']];
            $options['order_invoice__is_invoicing_options'] = $order_invoice__is_invoicing_option;
            View::assign($options);
            return View::fetch();
        }
    }
    /**
     * 删除
     *
     * admin-OrderInvoice-delete
     */
    public function delete()
    {
        \app\common\model\OrderInvoice::destroy(input('order_invoice__id'));
        return success();
    }
    /**
     * 列表
     *
     * admin-OrderInvoice-list
     */
    public function list()
    {
        if (request()->isPost()) {
            return error('请求方式错误');
        }
        $query = Db::table('db_order_invoice');
        if (input('order_invoice__create_time_start', '') || input('order_invoice__create_time_end', '')) {
            $order_invoice__create_time_start = input('order_invoice__create_time_start', '', 'search_in');
            $order_invoice__create_time_end = input('order_invoice__create_time_end', '', 'search_in');
            $order_invoice__create_time_start = $order_invoice__create_time_start ? strtotime($order_invoice__create_time_start) : 0;
            $order_invoice__create_time_end = $order_invoice__create_time_end ? strtotime($order_invoice__create_time_end) + 24 * 60 * 60 - 1 : time();
            $query->where('db_order_invoice.create_time', 'between', [$order_invoice__create_time_start, $order_invoice__create_time_end]);
        }
        if (input('order_invoice__is_special_ticket', '') !== '') {
            $query->where('db_order_invoice.is_special_ticket', '=', input('order_invoice__is_special_ticket', '', 'search_in'));
        }
        if (input('order_invoice__member_id', '') !== '') {
            $query->where('db_order_invoice.member_id', '=', input('order_invoice__member_id', '', 'search_in'));
        }
        if (input('is_invoicing', '') !== '') {
            $query->where('db_order_invoice.is_invoicing', '=', input('is_invoicing',0, 'intval'));
        }else{
            $query->where('db_order_invoice.is_invoicing', '=', 0);
        }
        $count['hasCount']=Db::table('db_order_invoice')->where("db_order_invoice.is_invoicing=1")->count();
        $count['readyCount']=Db::table('db_order_invoice')->where("db_order_invoice.is_invoicing=0")->count();
        View::assign($count);
        $query->where('db_order_invoice.delete_time', '=', null);
        $query->field(['db_order_invoice.id' => 'order_invoice__id']);
        $query->field(['db_order_invoice.is_special_ticket' => 'order_invoice__is_special_ticket']);
        $query->field(['db_order_invoice.company_name' => 'order_invoice__company_name']);
        $query->field(['db_order_invoice.tax_id' => 'order_invoice__tax_id']);
        $query->field(['db_order_invoice.amount_of_money' => 'order_invoice__amount_of_money']);
        $query->field(['db_order_invoice.email' => 'order_invoice__email']);
        $query->field(['db_order_invoice.content' => 'order_invoice__content']);
        $query->field(['db_order_invoice.remarks' => 'order_invoice__remarks']);
        $query->field(['db_order_invoice.related_orders' => 'order_invoice__related_orders']);
        $query->field(['db_order_invoice.is_invoicing' => 'order_invoice__is_invoicing']);
        $query->field(['db_order_invoice.invoice_attachment' => 'order_invoice__invoice_attachment']);
        $query->order('db_order_invoice.id', 'desc');
        $data = $query->paginate(input('listRow') ?: 15);
        $data = ['data' => $data->items(), 'paginate' => $data->render()];
        View::assign($data);
        $options = [];
        $order_invoice__is_special_ticket_option = [['name' => '是', 'id' => '1'], ['name' => '否', 'id' => '0']];
        $options['order_invoice__is_special_ticket_options'] = $order_invoice__is_special_ticket_option;
        $order_invoice__member_id_option = \app\common\model\Member::column('name as name, id as id');
        $options['order_invoice__member_id_options'] = $order_invoice__member_id_option;
        $order_invoice__id_option = \app\common\model\OrderInvoice::column('name as name, id as id');
        $options['order_invoice__id_options'] = $order_invoice__id_option;
        $order_invoice__is_special_ticket_option = [['name' => '是', 'id' => '1'], ['name' => '否', 'id' => '0']];
        $options['order_invoice__is_special_ticket_options'] = $order_invoice__is_special_ticket_option;
        $order_invoice__is_invoicing_option = [['name' => '是', 'id' => '1'], ['name' => '否', 'id' => '0']];
        $options['order_invoice__is_invoicing_options'] = $order_invoice__is_invoicing_option;
        View::assign($options);
        return View::fetch();
    }
}